package tech.jabari.user.dao.impl;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import tech.jabari.user.dao.UserDao;
import tech.jabari.user.entity.User;
import tech.jabari.utils.JdbcUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Jabari Lu
 * @title: UserDaoImpl
 * @data 2020/8/4 14:25
 */
@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<User> findAllUser(Map<String,Object> paramsMap) {
        Integer pageSize = (Integer)paramsMap.get("pageSize");
        Integer beginNo = (Integer)paramsMap.get("beginNo");
        String username = (String)paramsMap.get("username");
        List<User> userList = new ArrayList<>();
        String sql = "select * from t_user where username like ? order by id desc limit ?,?";
        userList = jdbcTemplate.query(sql,
                                new BeanPropertyRowMapper<>(User.class),
                "%" + username + "%", beginNo, pageSize);
        return userList;
    }


    @Override
    public Integer findAllUserCount(Map<String, Object> paramsMap) {
        String username = (String)paramsMap.get("username");
        String sql = "select count(*) as cnt from t_user where username like ?";
        Long cnt = jdbcTemplate.queryForObject(sql,Long.class,"%" + username + "%");
        return cnt.intValue();
    }


    @Override
    public Integer deleteUser(Integer id) {
        String sql = "delete from t_user where id = ?";
        int r = jdbcTemplate.update(sql, id);
        return r;
    }


    /**
     * 根据id数组批量删除用户
     * @param delIds - String - 格式： 1,2,3
     * @return
     */
    @Override
    public Integer batchDeleteUser(String delIds) {
        String sql = "delete from t_user where id in ("+delIds+")";
        int r = jdbcTemplate.update(sql);
        return r;
    }


    @Override
    public User findById(Integer idInt) {
        String sql ="select * from t_user where id = ?";
        List<User> userList = jdbcTemplate.query(sql,
                new BeanPropertyRowMapper<>(User.class),
                idInt);
        if (null != userList && userList.size() > 0) {
            return userList.get(0);
        }
        return null;
    }


    @Override
    public List<User> selectUserByUsernameAndPwd(String username, String pwd) {
        return jdbcTemplate.query
                ("select * from t_user where username=? and pwd=?",
                        new BeanPropertyRowMapper<>(User.class),
                        username,pwd);
    }


    @Override
    public List<User> selectUserByUsername(String username) {
        String sql = "select * from t_user where username = ?";
        return jdbcTemplate.query(sql,
                new BeanPropertyRowMapper<>(User.class),
                username);
    }


    @Override
    public Integer insertUser(User user) {
        String sql = "insert into t_user(username,pwd,email,phone,sex,birth,headImg)values(?,?,?,?,?,?,?)";
        return jdbcTemplate.update(sql, user.getUsername(),user.getPwd(),
                user.getEmail(),user.getPhone(),user.getSex(),user.getBirthStr(),user.getHeadImg());

    }

    @Override
    public Integer updateUser(User user) {
        String sql = "update t_user set username = ?,pwd = ?,email=?,phone=?,sex=?,birth=?,headImg=? where id = ?";
        return jdbcTemplate.update(sql, user.getUsername(),user.getPwd(), user.getEmail(),
                user.getPhone(),user.getSex(),user.getBirthStr(),
                user.getHeadImg(),user.getId());

    }
}
